﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;

namespace tank
{
    class _Hero
    {
        public Point Location { get; set; }
        public Size Size { get; set; }
        public _Orientation Or { get; set; }
        public int Velocity { get; set; }
        public bool IsRunning { get; set; }
        public int Score { get; set; }
        public bool IsEmeny { get; set; }
        public Rectangle Rectangle
        {
            get { return new Rectangle(Location, Size); }
        }

        public _Bullet Bullet { get; set; }

        int maxWidth, maxHeight;

        public _Hero(Point startPosition, int maxWidth, int maxHeight)
        {
            Location = startPosition;
            Size = new Size(48, 48);
            Or = _Orientation.Up;
            Velocity = 3;

            Bullet = new _Bullet(maxWidth, maxHeight, Or);

            this.maxWidth = maxWidth;
            this.maxHeight = maxHeight;
        }

        public void Update(ref List<_Block> blocks, List<_Hero> heroes, ref List<_Enemy> enemys)
        {
            int dx = 0, dy = 0;

            if (IsRunning)
            {
                if (Or == _Orientation.Up)
                    dy = -Velocity;
                else
                    if (Or == _Orientation.Down)
                        dy = Velocity;
                    else
                        if (Or == _Orientation.Left)
                            dx = -Velocity;
                        else
                            if (Or == _Orientation.Right)
                                dx = Velocity;


                Rectangle rect = new Rectangle(Location, Size);
                rect.Offset(dx, dy);

                bool intersect = false;

                foreach (_Block block in blocks)
                {
                    if (block.BlockType != _BlockType.Place_Green)
                    {
                        if (block.BlockType == _BlockType.Base)
                            if (RectIntersect(rect, block.Rectangle))
                                intersect = true;
                        if (RectIntersect(new Rectangle(block.Location, block.Size), rect))
                            intersect = true;
                    }
                }

                if (!intersect)
                {
                    foreach (_Enemy enemy in enemys)
                    {
                        if (RectIntersect(new Rectangle(enemy.Location, enemy.Size), rect))
                            intersect = true;
                    }
                }

                if (rect.Top > 0 && rect.Bottom < maxHeight && rect.Left > 0 && rect.Right < maxWidth && !intersect)
                    Location = rect.Location;
            }

            Bullet.Update(ref blocks);
            for (int i = 0; i < enemys.Count; i++)
            {
                if (RectIntersect(Bullet.Rectangle, enemys[i].Rectangle))
                {
                    switch (enemys[i].LevelEnemy)
                    {
                        case 1: Score++; break;
                    }
                    enemys.RemoveAt(i);
                    i--;
                    Bullet.Reset();
                }
            }
        }

        Image Image()
        {
            Image temp = tank.Properties.Resources.heroImage;

            switch (Or)
            {
                case _Orientation.Up: break;
                case _Orientation.Down: temp.RotateFlip(RotateFlipType.Rotate180FlipNone); break;
                case _Orientation.Left: temp.RotateFlip(RotateFlipType.Rotate270FlipNone); break;
                case _Orientation.Right: temp.RotateFlip(RotateFlipType.Rotate90FlipNone); break;
            }

            return temp;
        }

        public void StartShoot()
        {
            Bullet = new _Bullet(maxWidth, maxHeight, Or);
            switch (Or)
            {
                case _Orientation.Down:
                case _Orientation.Up:
                    Bullet.StartShoot(Location, new Size(Size.Width, Size.Height+16), Or, false); break;
                case _Orientation.Left:
                case _Orientation.Right:
                    Bullet.StartShoot(Location, new Size(Size.Width+16, Size.Height), Or, false); break;

            }
        }
        
        public void Draw(Graphics graphics)
        {
            graphics.DrawImage(Image(), Location);
            if (Bullet.IsRunning)
                Bullet.Draw(graphics);
        }


        bool RectIntersect(Rectangle rect1, Rectangle rect2)
        {
            bool intersect = false;

            if (rect1.Left >= rect2.Left && rect1.Left <= rect2.Right && rect1.Top >= rect2.Top && rect1.Top <= rect2.Bottom
               || rect1.Left >= rect2.Left && rect1.Left <= rect2.Right && rect1.Bottom >= rect2.Top && rect1.Bottom <= rect2.Bottom
               || rect1.Right >= rect2.Left && rect1.Right <= rect2.Right && rect1.Top >= rect2.Top && rect1.Top <= rect2.Bottom
               || rect1.Right >= rect2.Left && rect1.Right <= rect2.Right && rect1.Bottom >= rect2.Top && rect1.Bottom <= rect2.Bottom)
                intersect = true;

            return intersect;
        }
    }
}
